package com.waitingresult.controller;

import cn.hutool.core.date.DateTime;
import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.waitingresult.ThreadSaveBatchService;
import com.waitingresult.entity.Student;
import com.waitingresult.mapper.StudentMapper;
import com.waitingresult.service.StudentService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @Author: WangShuai
 * @CreateTime: 2022-12-29  11:08
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "/druid123")
public class DruidStatController {
    @Resource
    private StudentService studentService;
    @GetMapping("/stat")
    public Object druidStat() {
        // 获取数据源的监控数据
        List<Map<String, Object>> dataSourceStatDataList = DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
        return dataSourceStatDataList;
    }

    @GetMapping("saveBatch")
    public boolean saveBatch() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(500);
        scheduledExecutorService.submit(new ThreadSaveBatchService(studentService));

        for (int i = 0; i < 5000; i++) {
            scheduledExecutorService.submit(new ThreadSaveBatchService(studentService));
        }
        return true;
    }

    @Scheduled(cron = "0/5 * * * * ?")
    private void demo(){
        System.err.println("定时输出"+ DateTime.now());
    }
}
